package me.leon.keeplive;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.ColorInt;
import androidx.annotation.ColorRes;

/** @author Leon data 2018-1-12 desc 状态栏工具类 */
public class StatusBarHelper {
    private static final int COLOR_DEFAULT = Color.parseColor("#40000000");

    private StatusBarHelper() {}

    /** 全屏 */
    @TargetApi(19)
    public static void setFullScreen(Activity activity) {
        activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //
        // activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_FULLSCREEN);//下拉不消失
    }

    /** 布局侵入状态栏 */
    @TargetApi(19)
    public static void setImmerse(Activity activity) {
        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup contentView = window.getDecorView().findViewById(Window.ID_ANDROID_CONTENT);
        contentView.getChildAt(0).setFitsSystemWindows(false);
    }

    /** 设置状态栏颜色 21以下无法设置 */
    public static void setStatusBarColor(Activity activity, @ColorRes int color) {
        Window window = activity.getWindow();
        Resources resources = activity.getResources();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(resources.getColor(color));
        } else {
            addFakeStatusBar(activity, color);
        }
    }

    /** desc 6.0以上原生支持, 以下兼容, 改变背状态栏字体颜色 */
    @TargetApi(19)
    public static void setDarkFontStatusBar(Activity activity) {

        // 当前手机版本为6.0及以上
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            activity.getWindow()
                    .getDecorView()
                    .setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
            return;
        } else if (Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23) { // 兼容5.x
            activity.getWindow().setStatusBarColor(COLOR_DEFAULT);
        }

        // 手机版本为4.4
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
                && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            addFakeStatusBarColorInt(activity, COLOR_DEFAULT);
        }
    }

    @TargetApi(23)
    public static void clearDarkFontFlag(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            View decorView = activity.getWindow().getDecorView();
            int origin = decorView.getSystemUiVisibility();
            decorView.setSystemUiVisibility(origin & ~View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }
    }

    /** 添加fakeStatusBar 兼容Api 19及以下 */
    public static void addFakeStatusBar(Activity activity, @ColorRes int color) {
        Resources resources = activity.getResources();

        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup systemContent = activity.findViewById(android.R.id.content);

        View statusBarView = new View(activity);
        ViewGroup.LayoutParams lp =
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
        statusBarView.setBackgroundColor(resources.getColor(color));

        systemContent.getChildAt(0).setFitsSystemWindows(true);

        systemContent.addView(statusBarView, 0, lp);
    }

    public static void addFakeStatusBarColorInt(Activity activity, @ColorInt int color) {

        Window window = activity.getWindow();
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        ViewGroup systemContent = activity.findViewById(android.R.id.content);

        View statusBarView = new View(activity);
        ViewGroup.LayoutParams lp =
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
        statusBarView.setBackgroundColor(color);

        systemContent.getChildAt(0).setFitsSystemWindows(true);

        systemContent.addView(statusBarView, 0, lp);
    }

    /** 增加View的paddingTop,增加的值为状态栏高度 (智能判断，并设置高度) */
    public static void setPaddingSmart(Context context, View view) {
        if (Build.VERSION.SDK_INT >= 19) {
            ViewGroup.LayoutParams lp = view.getLayoutParams();
            if (lp != null && lp.height > 0) {
                lp.height += getStatusBarHeight(context); // 增高
            }
            view.setPadding(
                    view.getPaddingLeft(),
                    view.getPaddingTop() + getStatusBarHeight(context),
                    view.getPaddingRight(),
                    view.getPaddingBottom());
        }
    }

    /** 获取状态栏高度 */
    public static int getStatusBarHeight(Context activity) {
        Resources resources = activity.getResources();

        return resources.getDimensionPixelSize(
                resources.getIdentifier("status_bar_height", "dimen", "android"));
    }
}
